package com.gylang.print.pdf.test.pdf;

import ch.qos.logback.core.util.CloseUtil;
import com.gylang.print.pdf.test.TestConstant;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.File;
import java.io.IOException;

/**
 * @author gylang
 * data 2021/6/3
 */
public class WriteText {

	public static void main(String[] args) {
		PDPageContentStream contentStream = null;
		try (PDDocument pdDocument = new PDDocument()) {
			PDPage pdPage = new PDPage();
			pdDocument.addPage(pdPage);
			contentStream = new PDPageContentStream(pdDocument, pdPage);
			contentStream.beginText();
			PDFont font = PDType1Font.HELVETICA_BOLD;
			contentStream.setFont(font, 10);
			contentStream.newLineAtOffset(25, 500);
			contentStream.showText("你好呀");
			contentStream.endText();
			CloseUtil.closeQuietly(contentStream);
			pdDocument.save(new File(TestConstant.PDF_BASE + "write.pdf"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
